#ifndef __EXCHANGE_H
#define __EXCHANGE_H

#include "connection.h"

class Connection;

class Exchange
{
 public:
  // Data
  int myID;
  double time;
  double original_time;
  char type;
  int bytes; 
  Connection* c;

  // Constructors
  Exchange();
  Exchange(double, char, int, int, Connection*);
  Exchange(string);
  Exchange(const Exchange&);

  int operator>(const Exchange&);
  int operator<(const Exchange&);
  friend ostream& operator<<(ostream&, Exchange);
};

Exchange::Exchange()
{
  type = 'N';
  myID = -1;
  time = -1;
  c = 0;
  bytes = 0;
  original_time = time;
}

Exchange::Exchange(double ti, char ty, int b, int myid, Connection* con)
{
  time = ti;
  type = ty;
  bytes = b;
  myID = myid;
  c = con;
  original_time = time;
}

Exchange::Exchange(string line)
{
  char* token, contents[MAXL];

  bytes = 0;

  strcpy(contents, line.c_str());
  token = strtok(contents, " ");
  int pass = 0;
  while (token != NULL)
    {
      switch(pass)
	{
	case 0: 
	  myID = atoi(token);
	  break;
	case 1:
	  time = atof(token);
	  original_time = time;
	  break;
	case 2:
	  type = token[0];
	  break;
	case 6:
	  bytes = atoi(token);
	  break;
	}
      pass++;
      token = strtok(NULL, " ");
    }
  //cout << "Time "<< time << " type " << type << " ID "<< myID << " bytes "<< bytes;
}

Exchange::Exchange(const Exchange& e)
{
  myID = e.myID;
  time = e.time;
  original_time = e.original_time;
  type = e.type;
  bytes = e.bytes;
  c = e.c;
}

int Exchange::operator>(const Exchange& e)
{
  return (time > e.time);
}

int Exchange::operator<(const Exchange& e)
{
  return (time < e.time);
}

ostream& operator<<(ostream& out, Exchange e)
{
  out<<e.myID<<":"<<e.time;
  return out;
}

#endif
